/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 */

package org.biojava.servlets.dazzle;

import java.io.*;
import java.net.URLConnection;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

//import org.biojava.bio.seq.*;
//import org.biojava.utils.xml.*;
import org.biojava.servlets.dazzle.datasource.*;


//import org.biojava.bio.seq.*;
//import org.biojava.utils.xml.*;
//import org.biojava.servlets.dazzle.datasource.*;
//import org.biojava.servlets.dazzle.*;
//import uk.ac.sanger.*;
import uk.ac.sanger.servlet.*;
import uk.ac.sanger.FeatureBean;
//import org.biojava.servlets.dazzle.datasource.SimpleWritebackSource;
//import org.biojava.servlets.dazzle.datasource.*;
//import org.biojava.servlets.dazzle.*;
//import org.biojava.utils.xml.XMLWriter;

public class WritebackHandler extends AbstractDazzleHandler {
   
	public static final String CREATE_CMD ="1";
	public static final String MODIFY_CMD ="2";
	public static final String DELETE_CMD ="3";
	
	public WritebackHandler() {
    	
        super(
     
            DazzleReferenceSource.class,
            new String[] {"writeback"},
            new String[] {"writeback/1.0"}
            
        );
        System.out.println("instanciating WritebackHandler");
    }
    
  //run as http://localhost:8088/ExampleDynamicWeb/wrtback/writeback
    public void run(
        DazzleServlet dazzle,
        DazzleDataSource dds,
        String cmd,
        HttpServletRequest req,
        DazzleResponse resp
    )
        throws  IOException, DataSourceException, ServletException, DazzleException
    {
    	//SPECIFY CORRECT localhost, usually 8080 
    	resp.sendRedirect("http://localhost:8088/writebackServlet/index.jsp");
  
    //	WritebackSource source = (WritebackSource) dds;
        
   // 	System.out.println(" Writeback test!" + cmd);
    	
    	// for test, save feature directly
    	/*FeatureBean f =new FeatureBean();
    	Random generator = new Random();
    	    	
    	// find out which command has been posted
    	if (cmd.equals("writeback")){	
    		
    		f.setFeatureid("feature"+generator.nextInt(100));
			f.setFeaturetype("type");
			f.setMethod("method");

			f.setSegment("segment");
			f.setStart("1");
			f.setEnd("2");
			f.setStrand("+");

			f.setPhase(".");
			f.setScore("10");
			f.setLinks("www");
			f.setGroups("group");
			f.setCommitMsg("message");
			f.setUser("asja.myopenid.com");
			f.setSystem_uri("CS_DS26");
    	}
    	
    	// send to server
    	saveFeature(f);*/
    	
    	//what is this???
    	//writeServer.writeback(features, command);
    	
    	   
    }

    /*public void saveFeature(FeatureBean f) {
		try {
			FeatureUpload test = new FeatureUpload();

			URLConnection connection = null ;
			OutputStreamWriter out = null;
			
			connection=test.getConnection("http://localhost:8088/writebackServlet/writebackDatabase");

			if (connection!=null)
				out= test.getOutputStream(connection);
			else System.out.println("No connection to the server!");
		
			String result=test.writeFeaturesHTTP(out,connection, f, CREATE_CMD);
			System.out.println("result "+result);
			
		}
		catch (Exception e) {
			e.printStackTrace();}
	}*/
	
    
}
